//调用支付宝支付
function fnaliPayPlus(req) {
    var aliPayPlus = api.require('aliPayPlus');
    aliPayPlus.payOrder({
        orderInfo: req
    }, function(ret) {
        if (ret) {
            if (ret.code) {
                var tCode = ret.code + "";
                var tMsg = '支付失败';
                var code = -1;
                switch (tCode) {
                    case '9000':
                        tMsg = '支付成功';
                        code = 1;
                        break;
                    case '8000':
                        tMsg = '正在处理中,请查询商户订单列表中订单的支付状态';
                        code = -1;
                        break;
                    case '4000':
                        tMsg = '支付失败';
                        code = 0;
                        break;
                    case '5000':
                        tMsg = '重复请求';
                        code = -1
                        break;
                    case '6001':
                        tMsg = '取消支付';
                        code = -1;
                        break;
                    case '6002':
                        tMsg = '网络连接出错';
                        code = 0;
                        break;
                    case '6004':
                        tMsg = '支付结果未知，请查询商户订单列表中订单的支付状态';
                        code = -1;
                        break;
                }
                fnopenAudit(code);
                api.toast({
                    msg: tMsg,
                    duration: 2000,
                    location: 'bottom',
                    global: true,
                });
            } else {
                fnopenAudit(0);
                api.toast({
                    msg: '支付失败',
                    duration: 2000,
                    location: 'bottom',
                    global: true,
                });
            }
        } else {
            fnopenAudit(0);
            api.toast({
                msg: '支付失败',
                duration: 2000,
                location: 'bottom',
                global: true,
            });
        }
        $S.CloseProgress()
    });
}

// 支付宝是否安装
function isAli() {

}
// 微信是否安装
function isWX(callback) {
    var destCilentFunction = api.require('wxPlus').isInstalled;
    destCilentFunction(function(ret, err) {
        var insTallStatus = ret.installed;
        if (!insTallStatus) {
            fnToast('请先安装微信');
        }else {
            callback();
        }
    });
}

//调用微信支付
function fnwxPayPlus(rets) {
    var callback = function() {
        var tParam = {
            apiKey: rets.appid,
            orderId: rets.prepayid,
            mchId: rets.mchId,
            nonceStr: rets.nonceStr,
            timeStamp: rets.timeStamp,
            package: rets.package,
            sign: rets.sign
        };

        var wxPayPlus = api.require('wxPayPlus');
        wxPayPlus.payOrder(tParam, function(ret, err) {
            $S.CloseProgress()
            console.log(JSON.stringify(ret))
            if (ret.status) {
                fnopenAudit(1);
            } else {
                if (err.code && '-2' == err.code) {
                    fnopenAudit(-1);
                    api.toast({
                        msg: '取消支付',
                        duration: 2000,
                        location: 'bottom',
                        global: true,
                    });
                } else {
                    fnopenAudit(0);
                    api.toast({
                        msg: '支付失败',
                        duration: 2000,
                        location: 'bottom',
                        global: true,
                    });
                }
            }
        });
    }
    isWX(callback); 
};

function fnApplePay(productId, orderNo) {
    $S.ShowProgress();
    var iap = api.require('iap');
    iap.getProducts({
        productIds: [ 'LEW30', 'LEW198' ]
    }, function(ret, err) {
        if (ret) {
            console.log(JSON.stringify(ret)+'===log');
        } else {
            console.log(JSON.stringify(err)+'===error');
        }
    });
    iap.purchase({
        productId: productId,
    }, function(ret, err) {
        if (ret) {
            console.log(JSON.stringify(ret));
            // alert(JSON.stringify(ret));
            if (ret.state == '1') {
                var verifyData = {
                    transactionId: ret.transactionId || "",
                    payload: ret.receipt || "",
                    orderNo: orderNo
                }
                $S.POST("payOrder/iosPayCallback", verifyData, function(ret, err) {
                    if (ret && ret.status == '200') {
                        fnopenAudit('1')
                    }
                    setTimeout(function() {
                        $S.CloseProgress();
                    }, 500)
                });
                // fnVerifyApplePay()
            } else if (ret.state == '2') {
                fnToast("交易失败");
                $S.CloseProgress();
            } else if (ret.state != '0') {
                fnToast(ret.errorMsg);
                $S.CloseProgress();
            }
        } else {
            fnToast(ret.errorMsg);
            $S.CloseProgress();
            console.log(JSON.stringify(err));
            // alert(JSON.stringify(err));
        }
    });
}
